package com.yjd.pub.device.service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.device.model.DeviceModel;
import com.yjd.comm.device.service.IDeviceService;
import com.yjd.comm.enums.DeviceStatus;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.device.dao.IDeviceDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <b>description</b>：设备<br>
 * <b>time</b>：2015-01-27 12:28:26 <br>
 * <b>author</b>：  renbangjie
 */
@Service("deviceService")
public class DeviceServiceImpl extends BaseServiceImpl implements IDeviceService {

	@Override
	public boolean updateDeviceNo(Long deviceId, String deviceNo) throws Exception {
		if(deviceId == null || StringUtils.isBlank(deviceNo)){
			FrameUtil.throwBaseException1("参数异常");
		}
		DeviceModel deviceModel = new DeviceModel();
		deviceModel.setDevice_id(deviceId);
		deviceModel.setDevice_no(deviceNo);
		if(this.update(deviceModel) != 1){
			FrameUtil.throwBaseException1("更新不唯一");
		}
		return true;
	}

	/**
	 * 插入
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public DeviceModel insert(DeviceModel model) throws Exception {
		return this.deviceDao.insertModel(model, true);
	}

	/**
	 * 更新
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(DeviceModel model) throws Exception {
		return this.deviceDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 *
	 * @param device_id
	 * @return
	 * @throws Exception
	 */
	public int delete(Long device_id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("device_id", device_id);
		return this.deviceDao.deleteModel(paramMap, true);
	}

	@Override
	public DeviceModel getDeviceModelByDeviceNo(String DeviceNo) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("device_no", DeviceNo);
		return super.getModelOne(paramMap, DbWREnums.WRITE);
	}


	@Override
	public DeviceModel getDeviceModelByUser(Long idUser) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("user_id", idUser);
		paramMap.put("inuse", DeviceStatus.enable.getCode());
		return super.getModelOne(paramMap, DbWREnums.WRITE);
	}


	@Override
	public boolean updateDeviceStatus(DeviceModel deviceModel) {
		return deviceDao.updateDeviceStatus(deviceModel) >= 0;
	}

	@Override
	public UserSession getMobileUser(Long idUser) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("idUser", idUser);
		return deviceDao.getMobileUser(paramMap);
	}

	@Resource
	private IDeviceDao deviceDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.deviceDao;
	}
}
